﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WomanMotion.WebApi.Model.Dtos.Authoritys;
using WomanMotion.WebApi.Model.Dtos.Carousel;
using WomanMotion.WebApi.Model.Dtos.Classify;
using WomanMotion.WebApi.Model.Dtos.Comment;
using WomanMotion.WebApi.Model.Entitys;
using WomanMotion.WebApi.Repository.IRepository;
using WomanMotion.WebApi.Repository.Repository;
using WomanMotion.WebApi.Service.IService;

namespace WomanMotion.WebApi.Service.Service
{
    public class AuthoritysService : BaseCRUDService<Authoritys, AuthoritysVM, UpdateAuthoritysDTO, CreatedAuthoritysDTO>, IAuthoritysService
    {
        private readonly IAuthoritysRepository _authoritysRepository;
        private readonly IControllerService _controllerService;
        private readonly IActionsService _actionsService;

        public AuthoritysService(ILogger<AuthoritysService> logger, IMapper mapper, IAuthoritysRepository authoritysRepository, IHttpContextAccessor httpContext, IControllerService controllerService, IActionsService actionsService) : base(logger, mapper, authoritysRepository, httpContext)
        {
            _authoritysRepository = authoritysRepository;
            _controllerService = controllerService;
            _actionsService = actionsService;
        }

        /// <summary>
        ///查询所有权限集合
        /// </summary>
        /// <returns></returns>
        public async Task<List<AuthoritysPageVM>> GetAuthoritysListAsync()
        {
            var res = await this._authoritysRepository.GetCommonListAsync<AuthoritysPageVM>(_authoritysRepository.GetDB().Queryable<Authoritys>()
                .InnerJoin<Role>((auth, role) => auth.Roleid == role.Id)
                .InnerJoin<Controller>((auth, role, conl) => auth.Controllerid == conl.Id)
                .InnerJoin<Actions>((auth, role, conl, act) => auth.Actionsid == act.Id)
                .Select((auth, role, conl, act) =>
            new AuthoritysPageVM()
            {
                Id = auth.Id,
                Roleid = role.Id,
                RoleCode = role.Code,
                RoleName = role.Name,
                Controllerid = auth.Controllerid,
                ControllerCode = conl.Code,
                ControllerName = conl.Name,
                Actionsid = auth.Actionsid,
                ActionsCode = act.Code,
                ActionsName = act.Name
            }).MergeTable());
            return res;
        }

        /// <summary>
        /// 查询符合条件的权限集合
        /// </summary>
        /// <param name="expression">查询条件</param>
        /// <returns></returns>
        public async Task<List<AuthoritysPageVM>> GetAuthoritysListAsync(Expression<Func<AuthoritysPageVM, bool>> expression)
        {
            var res = await this._authoritysRepository.GetCommonListAsync<AuthoritysPageVM>(_authoritysRepository.GetDB().Queryable<Authoritys>()
                .InnerJoin<Role>((auth, role) => auth.Roleid == role.Id)
                .InnerJoin<Controller>((auth, role, conl) => auth.Controllerid == conl.Id)
                .InnerJoin<Actions>((auth, role, conl, act) => auth.Actionsid == act.Id)
                .Select((auth, role, conl, act) =>
            new AuthoritysPageVM()
            {
                Id = auth.Id,
                Roleid = role.Id,
                RoleCode = role.Code,
                RoleName = role.Name,
                Controllerid = auth.Controllerid,
                ControllerCode = conl.Code,
                ControllerName = conl.Name,
                Actionsid = auth.Actionsid,
                ActionsCode = act.Code,
                ActionsName = act.Name
            }).MergeTable().Where(expression));
            return res;
        }

        /// <summary>
        ///查询符合条件的单条记录
        /// </summary>
        /// <param name="expression">查询条件</param>
        /// <returns></returns>
        public async Task<AuthoritysPageVM> GetAuthoritysFirstAsync(Expression<Func<AuthoritysPageVM, bool>> expression)
        {
            var res = await this._authoritysRepository.GetCommonFirstAsync<AuthoritysPageVM>(_authoritysRepository.GetDB().Queryable<Authoritys>()
                .InnerJoin<Role>((auth, role) => auth.Roleid == role.Id)
                .InnerJoin<Controller>((auth, role, conl) => auth.Controllerid == conl.Id)
                .InnerJoin<Actions>((auth, role, conl, act) => auth.Actionsid == act.Id)
                .Select((auth, role, conl, act) =>
            new AuthoritysPageVM()
            {
                Id = auth.Id,
                Roleid = role.Id,
                RoleCode = role.Code,
                RoleName = role.Name,
                Controllerid = auth.Controllerid,
                ControllerCode = conl.Code,
                ControllerName = conl.Name,
                Actionsid = auth.Actionsid,
                ActionsCode = act.Code,
                ActionsName = act.Name
            }).MergeTable().Where(expression));
            return res;
        }

        /// <summary>
        ///同步符合条件的多表连接单条记录
        /// </summary>
        /// <param name="expression">查询条件</param>
        /// <returns></returns>
        public AuthoritysPageVM GetAuthoritysFirst(Expression<Func<AuthoritysPageVM, bool>> expression)
        {
            var res = this._authoritysRepository.GetCommonFirst<AuthoritysPageVM>(_authoritysRepository.GetDB().Queryable<Authoritys>()
                .InnerJoin<Role>((auth, role) => auth.Roleid == role.Id)
                .InnerJoin<Controller>((auth, role, conl) => auth.Controllerid == conl.Id)
                .InnerJoin<Actions>((auth, role, conl, act) => auth.Actionsid == act.Id)
                .Select((auth, role, conl, act) =>
            new AuthoritysPageVM()
            {
                Id = auth.Id,
                Roleid = role.Id,
                RoleCode = role.Code,
                RoleName = role.Name,
                Controllerid = auth.Controllerid,
                ControllerCode = conl.Code,
                ControllerName = conl.Name,
                Actionsid = auth.Actionsid,
                ActionsCode = act.Code,
                ActionsName = act.Name
            }).MergeTable().Where(expression));
            return res;
        }

        /// <summary>
        ///获取权限Tree数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<AuthoritysTreeVM>> GetTreeListAsync()
        {
            //获取控制器集合
            var res = new List<AuthoritysTreeVM>();
            var controllers = await _controllerService.GetListAsync();
            foreach (var controller in controllers)
            {
                var controllerTree = new AuthoritysTreeVM() { id = controller.Id, parentId = 0, value = "controller," + controller.Id.ToString(), label = controller.Name };
                res.Add(controllerTree);
                //查询控制器下属的Actions
                var actionss = await _actionsService.GetListAsync(m => m.Controllerid == controller.Id);
                foreach (var action in actionss)
                {
                    var actionTree = new AuthoritysTreeVM() { id = action.Id, parentId = controller.Id, value = "action," + action.Id.ToString(), label = action.Name };
                    res.Add(actionTree);
                }
            }
            return res;
        }
    }
}